﻿using Android.Content;
using Android.Net.Wifi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace NightPlayer.windows.utils
{
    public class UDPMusicSwitcher : IDisposable
    {
        private Context mContext;
        private Thread mThread;

        CancellationTokenSource mToken;

        public static int listenPort = 38247;

        public static UdpClient socket;

        WifiManager wifiManager;
        WifiManager.WifiLock wifiLock;
        const string lockTag = "NightPlayer";
        public UDPMusicSwitcher(Context context)
        {
            mContext = context;
            wifiManager = context.GetSystemService(Context.WifiService) as WifiManager;
            wifiLock = wifiManager.CreateWifiLock(Android.Net.WifiMode.Full,lockTag);
            
        }

        public void start()
        {
            mToken = new CancellationTokenSource();
            mThread = new Thread(serverThread);
            mThread.Start(); 
            wifiLock.Acquire();
        }

        private void serverThread()
        {
            CancellationToken cancellationToken = mToken.Token;
            try
            {
                socket = new UdpClient(new IPEndPoint(IPAddress.Any, listenPort));
                while (true) {
                    IPEndPoint ipe = new IPEndPoint(IPAddress.Any, listenPort);
                    var data = socket.Receive(ref ipe);
                    if(data != null)
                    {
                        if (data.Length == 4 && data[0] == 0xf0 && data[1] == 0x8d && data[2] == 0x19)
                        {
                            int action = data[3];
                            if (action == 0) {
                                mContext.SendBroadcast(new Intent(PlayerService.NOTI_PREV));
                            }
                            if (action == 1)
                            {
                                mContext.SendBroadcast(new Intent(PlayerService.NOTI_PLAY));
                            }
                            if (action == 2)
                            {
                                mContext.SendBroadcast(new Intent(PlayerService.NOTI_NEXT));
                            }
                            if (action == 3)
                            {
                                mContext.SendBroadcast(new Intent(PlayerService.NOTI_VOLUP));
                            }
                            if (action == 4)
                            {
                                mContext.SendBroadcast(new Intent(PlayerService.NOTI_VOLDN));
                            }
                            if (action == 5)
                            {
                                mContext.SendBroadcast(new Intent(PlayerService.NOTI_NOISE));
                            }
                            if (action == 6)
                            {
                                mContext.SendBroadcast(new Intent(PlayerService.NOTI_BBT));
                            }
                            if (action == 7)
                            {
                                mContext.SendBroadcast(new Intent(PlayerService.NOTI_PREVFOLDER));
                            }
                            if (action == 8)
                            {
                                mContext.SendBroadcast(new Intent(PlayerService.NOTI_NEXTFOLDER));
                            }
                        }
                    }
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
                stop();
            }
        }

        public void stop()
        {
            try
            {

                mToken.Cancel();
            }
            catch { }
            try
            {

                mThread.Interrupt();
            }
            catch { }
            try
            {

                socket.Close();
            }
            catch { }
            try
            {
                wifiLock?.Release();
            }
            catch { }
        }


        public void Dispose()
        {
            stop();
        }
    }
}
